﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CloudDcPc.util
{
    class StringUtil
    {
        public static string getFullTimeStr()
        {
            System.DateTime cTime = new System.DateTime();
            cTime = System.DateTime.Now;
            return cTime.ToString("yyyyMMddHHmmss");///////////////                     
        }

        public static double getNowTimeStamp()
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return double.Parse(((System.DateTime.Now - startTime).TotalSeconds+"").Split('.')[0]);       
        }

        public static string GetMD5(string sDataIn)
        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] bytValue, bytHash;

            bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);

            bytHash = md5.ComputeHash(bytValue);

            md5.Clear();

            string sTemp = "";

            for (int i = 0; i < bytHash.Length; i++)
            {

                sTemp += bytHash[i].ToString("X").PadLeft(2, '0');

            }

            return sTemp.ToLower();

        }

        /// <summary>
        /// 获取拆分后的字符串列表。
        /// </summary>
        /// <param name="mOrigianlString"></param>
        /// <param name="subStringCharNumber"></param>
        /// <returns></returns>
        private ArrayList GetSeparateSubString(string mOrigianlString, int subStringCharNumber)
        {
            ArrayList resultList = new ArrayList();
            string tempStr = mOrigianlString;
            int charNumber = subStringCharNumber;
            int totalCount = 0;
            string mSubStr = "";
            for (int i = 0; i < tempStr.Length; i++)
            {
                string mChar = tempStr.Substring(i, 1);
                int byteCount = Encoding.Default.GetByteCount(mChar);//关键点判断字符所占的字节数

                if (byteCount == 1)
                {
                    totalCount++;
                    mSubStr += mChar;
                    if (totalCount == charNumber || i == tempStr.Length - 1)
                    {
                        resultList.Add(mSubStr);
                        totalCount = 0;
                        mSubStr = "";
                    }
                }
                else if (byteCount > 1)
                {

                    totalCount += 2;
                    if (totalCount > charNumber)
                    {
                        resultList.Add(mSubStr);
                        if (i == tempStr.Length - 1)
                        {
                            mSubStr = mChar;
                            resultList.Add(mSubStr);
                        }
                        else
                        {
                            totalCount = 2;
                            mSubStr = mChar;
                        }
                    }
                    else if (totalCount == charNumber)
                    {
                        mSubStr += mChar;
                        resultList.Add(mSubStr);
                        totalCount = 0;
                        mSubStr = "";
                    }
                    else if (i == tempStr.Length - 1)
                    {
                        mSubStr += mChar;
                        resultList.Add(mSubStr);
                    }
                    else
                    {
                        mSubStr += mChar;
                    }
                }
            }
            return resultList;
        }


        public string FillStr(string from, int len, string filler)
        {
            for (int i = len - from.Length; i > 0; i--)
            {
                from += filler;
            }
            return from;
        }

        public double Round(double v, int x)
        {
            bool isNegative = false;
            //如果是负数 
            if (v < 0)
            {
                isNegative = true;
                v = -v;
            }

            int IValue = 1;
            for (int i = 1; i <= x; i++)
            {
                IValue = IValue * 10;
            }
            double Int = Math.Round(v * IValue + 0.5, 0);
            v = Int / IValue;

            if (isNegative)
            {
                v = -v;
            }

            return v;
        }
    }
}
